import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.UUID;

@MultipartConfig//[当前的类为一个文件上传的类]
@WebServlet("/upfile")
public class FileServlet extends HttpServlet {
   //1.上传文件要使用post类型，因为get类型有大小限制
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //2.得到客户端上传的文件
        Part part=req.getPart("name");
        //2.1每次得到一个唯一的文件【不要用时间戳】
        String fileName= UUID.randomUUID().toString();
        //2.2得到文件类型
        String fileType=part.getSubmittedFileName().substring(
                part.getSubmittedFileName().lastIndexOf("."));
        //3.将文件保存到服务器
     part.write("D:\\"+fileName+fileType);
    }

//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
////        super.doGet(req, resp);
//    }
}
